package com.easycarebn.main.favorites;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.easycarebn.main.adapter.SearchResultAdapter;
import com.easycarebn.main.utils.IResult;
import com.easycarebn.main.utils.ItemDr;
import com.easycarebn.main.utils.Utils;
import com.easycarebn.request.controller.ServerController;
import com.easycarebn.request.data.DoctorInfo;
import com.easycarebn.request.data.DoctorInfo.Booking;
import com.easycarebn.request.data.DoctorInfoResponse;
import com.easycarebn.request.data.DoctorScheduleResponse;
import com.easycarebn.request.data.UserResponse;
import com.easycarebn.service.request.OnRequest;
import com.timbacsi.main.MainActivity;
import com.timbacsi.main.R;

public class Drfavorites implements IResult {
    private View mView;
    private Activity mActivity;
    private SearchResultAdapter adapter;
    private List<ItemDr> listItemDrs;
    private ListView listmessage;
    private View mFooter;
    private int mType;

    private int mPage = 0;
    private int mLastPage = 0;
    private int mSelectedDoctorId;
    private ItemDr itemDr = null;
    private ArrayList<ItemDr> listItemNews = new ArrayList<ItemDr>();

    public Drfavorites(int type, Activity activity, View view) {
        this.mView = view;
        this.mActivity = activity;
        this.mType = type;

        mFooter = mActivity.getLayoutInflater().inflate(R.layout.load_more, null);
        adapter = new SearchResultAdapter(mActivity, listItemNews, this);
        listmessage = (ListView) mView.findViewById(R.id.listmessage);
        listmessage.setAdapter(adapter);
        listmessage.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mSelectedDoctorId = adapter.getItem(position).getInfo().getId();
                itemDr = adapter.getItem(position);
                Bundle data = new Bundle();
                data.putInt(Utils.KEY_DOCTOR_ID, mSelectedDoctorId);
                data.putInt(Utils.KEY_SERVICE_TYPE, mType);
                Utils.intentToDetailt(mActivity, Utils.DOCTOR_SERVICE_DETAILT, data);
            }

        });
        onRequest();

    }

    public void onRequest() {
        UserResponse user = Utils.getUserLogin(mActivity);
        MainActivity.mLoadingData.setVisibility(View.VISIBLE);
        ServerController.getInstance(mActivity).requestGetListFavorites(user.getToken(), mType, "", mPage,
                new OnRequest<DoctorInfoResponse>() {

                    @Override
                    public void onSuccess(DoctorInfoResponse data) {
                        MainActivity.mLoadingData.setVisibility(View.GONE);
                        if (data.getItems() == null || data.getItems().size() == 0) {
                            Utils.onShowMessageNull(mActivity, mView,
                                    mActivity.getResources().getString(R.string.vui_long_yeu_thich));

                            return;
                        }
                        mPage = data.getPaging().getCurrentPage();
                        mLastPage = data.getPaging().getLastPage();
                        if (mLastPage > mPage) {
                            if (listmessage.getFooterViewsCount() == 0)
                                listmessage.addFooterView(mFooter);
                        } else {
                            if (listmessage.getFooterViewsCount() > 0)
                                listmessage.removeFooterView(mFooter);
                        }
                        for (DoctorInfo info : data.getItems()) {
                            ItemDr item = new ItemDr();
                            item.setDatkham(true);
                            item.setInfo(info);
                            listItemNews.add(item);
                        }
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onError(int errorCode, String message) {
                        MainActivity.mLoadingData.setVisibility(View.GONE);

                        if (errorCode == 0) {
                            Utils.onResumeRequest(mActivity, mView, new IResult() {
                                @Override
                                public void onDisplay(int arg1, String arg2) {
                                    onRequest();
                                }
                            });
                        }
                    }
                });
    }

    @Override
    public void onDisplay(int code, String arg2) {

        if (adapter == null) {
            return;
        }
        if (code == Utils.SELECT_TIME_BOOKING) {
            Utils.gotoBooking(mActivity, arg2, mType);

            return;
        }

        int position = code;
        final DoctorInfo doctor = adapter.getItem(position).getInfo();
        Booking booking = doctor.getBooking();
        if (booking == null || TextUtils.isEmpty(booking.getNeardate())) {
            // Show dat lich truoc
            Utils.showDatlichtruoc(mActivity, doctor);
            return;
        }
        // Hien thi lich kham
        MainActivity.mLoadingData.setVisibility(View.VISIBLE);
        ServerController.getInstance(mActivity).requestSearchDoctorSchedule(doctor.getId(), booking.getNeardate(),
                new OnRequest<DoctorScheduleResponse>() {

                    @Override
                    public void onSuccess(DoctorScheduleResponse data) {

                        MainActivity.mLoadingData.setVisibility(View.GONE);
                        if (data == null || data.getItems() == null || data.getItems().isEmpty()) {
                            Utils.showMessage(mActivity, R.string.khong_co_lich_kham);
                        } else {
                            Utils.getDialogTimeBook(mActivity, Drfavorites.this, doctor.getId(), data.getItems(), data);
                        }
                    }

                    @Override
                    public void onError(int errorCode, String message) {

                        MainActivity.mLoadingData.setVisibility(View.GONE);

                    }
                });
    }

}
